<?php
    include("include/header.inc.php");
    include('include/func.inc.php');
?>
<div data-role="content" >	
    <h2><?php echo $texts['login'][$_SESSION['lang']] ?></h2>
    <form method='POST' >
        <h3><?php echo $texts['employee'][$_SESSION['lang']] ?></h3>
        <?php echo $texts['username'][$_SESSION['lang']] ?>*:
        <input type='text' name='username' id='username'  maxlength="50" />
        <?php echo $texts['password'][$_SESSION['lang']] ?>*:
        <input type='password' name='password' id='password' maxlength="50" />
        <h3><?php echo $texts['invitation'][$_SESSION['lang']] ?></h3>
        <?php echo $texts['key'][$_SESSION['lang']] ?>*:
        <input type='text' name='key' id='key'  maxlength="50" />
        <center><input type="submit" data-inline="true" align="center" value='<?php echo $texts['login'][$_SESSION['lang']] ?>'/></center>
    </form>
    
    <?php
        //echo 'clé d\'exemple : 154789685398FG45';
        if(!(empty($_POST['username']) && empty($_POST['password']))){
            $username = $_POST['username'];
            $password = $_POST['password'];

            $info = connectLDAP($username, $password); 
            
            if ($info != 'Erreur'){    
                // Fill session variables
                $sn = htmlentities($info[0]["sn"][0], ENT_QUOTES, 'UTF-8') ;//nom
                $givenname = htmlentities($info[0]["givenname"][0], ENT_QUOTES, 'UTF-8');//prénom
                $mail = htmlentities($info[0]["mail"][0], ENT_QUOTES, 'UTF-8');//mail
                $login = htmlentities($info[0]["uid"][0], ENT_QUOTES, 'UTF-8');//login
                $street = htmlentities($info[0]["postalAddress"][0], ENT_QUOTES, 'UTF-8');//adresse
                $fax = htmlentities($info[0]["facsimileTelephoneNumber"][0], ENT_QUOTES, 'UTF-8');//fax
                $phone = htmlentities($info[0]["telephoneNumber"][0], ENT_QUOTES, 'UTF-8');//Téléphone
                $mobile = htmlentities($info[0]["mobile"][0], ENT_QUOTES, 'UTF-8');//Mobile
                $city = htmlentities($info[0]["l"][0], ENT_QUOTES, 'UTF-8');//Ville
                $zipCode = htmlentities($info[0]["postalCode"][0], ENT_QUOTES, 'UTF-8');//ZIP code
                $institution = htmlentities($info[0]["o"][0], ENT_QUOTES, 'UTF-8') ;//Institution
                $country = htmlentities($info[0]["st"][0], ENT_QUOTES, 'UTF-8');//Pays 
                
                
                echo "$login/$zipCode/$country/$phone/$fax";
                
                $_SESSION['name'] = $givenname.' '.$sn;
                $_SESSION['login'] = $login;
                $_SESSION['mail']  = $mail;
                
                //Test si le compte LDAP existe dans le système SAP et le crée si ce n'est pas le cas.
                
                //SAP connection
                $rfc = connectSAP();
                if(!$rfc){
                    echo "The RFC connection has failed with the following error:".saprfc_error();
                    exit;
                }
                //-- FUNCTION MODULE: Z_FM_M646_SET_PERSON_LDAP --
                //We must know if the function really exists
                $fce_personLDAP_new = saprfc_function_discover($rfc, "Z_FM_M646_SET_PERSON_LDAP");
                if(!$fce_personLDAP_new){
                    echo "The function module has failed.";
                    echo $rfc;
                    exit;
                }

                //Pass import parameters
                saprfc_import ($fce_personLDAP_new,"IV_MEMBER", 
                                array ("USERNAME"=>strtoupper($login),
                                       "LASTNAME"=>$sn, 
                                       "FIRSTNAME"=>$givenname, 
                                       "ADDRESS"=>$street,
                                       "ZIP_CODE"=>$zipCode,
                                       "CITY"=>$city, 
                                       "EMAIL"=>$mail, 
                                       "PHONE"=>$phone,
                                       "MOBILE_PHONE"=>$mobile,
                                       "COUNTRY"=>strtoupper($country),
                                       "FAX"=>$fax,  
                                       "INSTITUTION"=>$institution));

                //Call and execute the function
                $rc_registration_new = saprfc_call_and_receive ($fce_personLDAP_new);            
                if ($rc_registration_new != SAPRFC_OK){
                    if ($rfc == SAPRFC_EXCEPTION )
                        echo ("Exception raised: ".saprfc_exception($fce_personLDAP_new));
                    else
                        echo ("Call error: ".saprfc_error($fce_personLDAP_new));
                    exit;
                }

                //realease the function
                saprfc_function_free($fce_personLDAP_new);
                echo '<script>location.href="index.php"</script>';
            }
            else
                echo 'Erreur lors de la connexion';       
        }
        else if(!(empty($_POST['key']))){
            
            $key = strtoupper($_POST['key']); //Clé de l'event 1 : 154789685398FG45
            
            $LOGIN = array ("ASHOST"=>"153.109.124.33",
                                "SYSNR"=>"D07",
                                "CLIENT"=>"100",
                                "USER"=>"M646-04",
                                "PASSWD"=>"hevs12",
                                "CODEPAGE"=>"1100");
            //Try to connect to SAP using our Login array
            $rfc = saprfc_open ($LOGIN);
            if(!$rfc){
                echo "The RFC connection has failed with the following error:".saprfc_error();
                exit;
            }			

            //We must know if the function really exists
            $fce = saprfc_function_discover($rfc, "Z_FM_M646_GET_EVENTS");
            if(!$fce){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }    

            //Pass import parameters
            //saprfc_import ($fce,"IP_THEMATIC",$thematic);

            //Pass table parameters
            saprfc_table_init ($fce,"GET_EVENTS");

            //Call and execute the function
            $rc = saprfc_call_and_receive ($fce);            
            if ($rfc_rc != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION )
                    echo ("Exception raised: ".saprfc_exception($fce));
                else
                    echo ("Call error: ".saprfc_error($fce));
                exit;
            }

            //Fetch the data from the internal tables
            $data_row = saprfc_table_rows ($fce,"GET_EVENTS");

            $idEvent ='';
            for ($i=1; $i<=$data_row; $i++){
                $DATARow = saprfc_table_read ($fce,"GET_EVENTS",$i);
                //Get the correct date format

                if ($key == $DATARow['KEYREGISTER'])
                    $idEvent = $DATARow['ID'];
            }

            //realease the function and close the connection
            saprfc_function_free($fce);
            saprfc_close($rfc);
            if ($idEvent <>''){
                $_SESSION['login'] = $idEvent;
                echo '<script>location.href="detailEvent.php?event='.$idEvent.'"</script>';
            }
            else
                echo '<p align="center"><font color="red"><b>'.$texts['invalid_key'][$_SESSION['lang']].'</b></font></p>';
        } 
    ?>

</div>


<?php
    include('include/footer.inc.php');  // Nous appelons notre menu
?>